# !/bin/bash

# Constants
host_url="git@gitlab.tsingmicro.com:tx8_toolchain/tx8-host.git"
host_dir="$project_dir/third-party/tx8-host"
host_checkout_433="228e2f"
hrt_runtime_dir="$host_dir/host-runtime"

host_release_dir="$host_dir/host-runtime/output/lib"
host_lib="$host_release_dir/libtx8_runtime.so"
host_include_dir="$hrt_runtime_dir/include/host"
host_include_file="$hrt_runtime_dir/include/host/host_runtime.h"

echo -e "Using tx8_host DIR_PATH: \033[32m $host_dir \033[0m"

##########  clone hostruntime  ##########
# host_rt_dir="$project_dir/$host_dir"
# if [ ! -d "$host_rt_dir" ]; then
#     pushd "$project_dir/third-party" >/dev/null 2>&1
#     if command -v git >/dev/null 2>&1 ; then
#         echo -e "Cloning  : \033[47;34m $host_git \033[0m to \033[47;34m third-party \033[0m"
#         git clone --recursive "$host_git" >/dev/null 2>&1
#         pushd "$host_rt_dir" >/dev/null 2>&1
#         git config --global --add safe.directory "*"
#         echo -e "Checkout : \033[47;34m $host_rt_dir   \033[0m to \033[47;34m $host_checkout_433     \033[0m"
#         git checkout "$host_checkout_433" >/dev/null 2>&1
#         popd >/dev/null 2>&1
#     else
#         echo "git not install"
#     fi
#     popd >/dev/null 2>&1
# fi

function build_target_hrt() {
    local repo_dir="$1"
    local release_dir="$2"
    
    if [ -d "$release_dir" ]; then
        # echo -e "Release DIR_PATH: \033[47;34m $release_dir \033[0m exists."
        echo -e "Release DIR_PATH: \033[34m $release_dir \033[0m exists."
    fi

    if [ -f "$host_lib" ]; then
        echo -e "Library: \033[34m $host_lib \033[0m exists, Using this lib."
        return 0
    fi

    echo -e "Building : \033[34m $repo_dir   \033[0m"
    pushd ${repo_dir} >/dev/null 2>&1
    source build.sh >/dev/null 2>&1
    popd >/dev/null 2>&1
}


function main_hrt() {
    check_repo_exists $host_dir $host_url

    if [ $? -eq 0 ]; then
        pushd "$host_dir" >/dev/null 2>&1
        git checkout "$host_checkout_433" >/dev/null 2>&1
        echo -e "Checkout : $host_checkout_433 ."
        popd >/dev/null 2>&1
    fi

    build_target_hrt $hrt_runtime_dir $host_release_dir
}

main_hrt